package net.shopxx.action.admin;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.shopxx.entity.Brand;
import net.shopxx.entity.Indexfloor;
import net.shopxx.entity.Indexfloortab;
import net.shopxx.entity.Product;
import net.shopxx.entity.ProductCategory;
import net.shopxx.service.BrandService;
import net.shopxx.service.IndexfloorService;
import net.shopxx.service.IndexfloortabService;
import net.shopxx.service.ProductCategoryService;
import net.shopxx.service.ProductService;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 后台Action类 - 首页楼层tab
 */

@ParentPackage("admin")
public class IndexfloortabAction extends BaseAdminAction {

	private static final long serialVersionUID = -7712786079159509171L;

	private Indexfloortab indexfloortab;
	private String indexfloorId;
	private Indexfloor indexfloor;
	
	private String[] productIds;

	@Resource
	private IndexfloortabService indexfloortabService;
	@Resource
	private IndexfloorService indexfloorService;
	@Resource
	ProductCategoryService productCategoryService;
	@Resource
	BrandService brandService;
	@Resource
	ProductService productService;   
	// 根据indexfloorId获取已启用的商品属性JSON数据
	public String ajaxIndexfloortab() {
		Indexfloor indexfloor = indexfloorService.load(indexfloorId);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[] { "createDate", "modifyDate", "indexfloor" });
		JSONArray jsonArray = JSONArray.fromObject(indexfloor.getEnabledIndexfloortabList(), jsonConfig);
		return ajaxJson(jsonArray.toString());
	}

	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		indexfloortab = indexfloortabService.load(id);
		return INPUT;
	}

	// 列表
	public String list() {
		if (StringUtils.isNotEmpty(indexfloorId)) {
			indexfloor = indexfloorService.load(indexfloorId);
			DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Indexfloortab.class);
			detachedCriteria.add(Restrictions.eq("indexfloor", indexfloor));
			pager = indexfloortabService.findByPager(pager, detachedCriteria);
		} else {
			pager = indexfloortabService.findByPager(pager);
		}
		return LIST;
	}

	// 删除
	public String delete() {
		indexfloortabService.delete(ids);
		return ajaxJsonSuccessMessage("删除成功！");
	}

	// 保存
	@Validations(
		requiredStrings = {
			@RequiredStringValidator(fieldName = "indexfloortab.name", message = "楼层tab名称不允许为空!"), 
			@RequiredStringValidator(fieldName = "indexfloortab.indexfloor.id", message = "首页楼层不允许为空!") 
		}, 
		requiredFields = {
			@RequiredFieldValidator(fieldName = "indexfloortab.isEnabled", message = "是否启用不允许为空!"),
			@RequiredFieldValidator(fieldName = "indexfloortab.orderList", message = "排序不允许为空!") 
		}, 
		intRangeFields = {
			@IntRangeFieldValidator(fieldName = "indexfloortab.orderList", min = "0", message = "排序必须为零或正整数!") 
		}
	)
	@InputConfig(resultName = "error")
	public String save() {
	    if (productIds != null && productIds.length > 0) {
	        List<Product> productList = new ArrayList<Product>(productService.get(productIds));
            indexfloortab.setProductList(productList);
        } else {
            indexfloortab.setProductList(null);
        }
	    
		indexfloortabService.save(indexfloortab);
		if (StringUtils.isNotEmpty(indexfloorId)) {
			redirectionUrl = "indexfloortab!list.action?indexfloorId=" + indexfloortab.getIndexfloor().getId();
		} else {
			redirectionUrl = "indexfloortab!list.action";
		}
		return SUCCESS;
	}

	// 更新
	@Validations(
        requiredStrings = {
            @RequiredStringValidator(fieldName = "indexfloortab.name", message = "楼层tab名称不允许为空!"), 
            @RequiredStringValidator(fieldName = "indexfloortab.indexfloor.id", message = "首页楼层不允许为空!") 
        }, 
        requiredFields = {
            @RequiredFieldValidator(fieldName = "indexfloortab.isEnabled", message = "是否启用不允许为空!"),
            @RequiredFieldValidator(fieldName = "indexfloortab.orderList", message = "排序不允许为空!") 
        }, 
        intRangeFields = {
            @IntRangeFieldValidator(fieldName = "indexfloortab.orderList", min = "0", message = "排序必须为零或正整数!") 
        }
    )
	@InputConfig(resultName = "error")
	public String update() {
	    
	    if (productIds != null && productIds.length > 0) {
            List<Product> productList = new ArrayList<Product>(productService.get(productIds));
            indexfloortab.setProductList(productList);
        } else {
            indexfloortab.setProductList(null);
        }
	    
		Indexfloortab persistent = indexfloortabService.load(id);
		
		BeanUtils.copyProperties(indexfloortab, persistent, new String[] {"id", "createDate", "modifyDate"});
		indexfloortabService.update(persistent);
		if (StringUtils.isNotEmpty(indexfloorId)) {
			redirectionUrl = "indexfloortab!list.action?indexfloorId=" + indexfloortab.getIndexfloor().getId();
		} else {
			redirectionUrl = "indexfloortab!list.action";
		}
		return SUCCESS;
	}

    public Indexfloortab getIndexfloortab() {
        return indexfloortab;
    }

    public void setIndexfloortab(Indexfloortab indexfloortab) {
        this.indexfloortab = indexfloortab;
    }

    public String getIndexfloorId() {
        return indexfloorId;
    }

    public void setIndexfloorId(String indexfloorId) {
        this.indexfloorId = indexfloorId;
    }

    public Indexfloor getIndexfloor() {
        return indexfloor;
    }

    public void setIndexfloor(Indexfloor indexfloor) {
        this.indexfloor = indexfloor;
    }

 // 获取所有首页楼层
    public List<Indexfloor> getAllIndexfloor() {
        return indexfloorService.getAll();
    }
    
 // 获取商品分类树
    public List<ProductCategory> getProductCategoryTreeList() {
        return productCategoryService.getProductCategoryTreeList();
    }
    // 获取所有品牌
    public List<Brand> getAllBrand() {
        return brandService.getAll();
    }

    public String[] getProductIds() {
        return productIds;
    }

    public void setProductIds(String[] productIds) {
        this.productIds = productIds;
    }
	

}